<?php
/**
+--------------------------------------------------------------
 * User: maqun
+--------------------------------------------------------------
 * Date: 2015/12/5
+--------------------------------------------------------------
 * Time: 14:58
+--------------------------------------------------------------
 * 用户信息
+--------------------------------------------------------------
 */
class UsersModel extends BaseModel{

    protected $tableName = "users";//数据库表名

    protected $_validate = array(
        array('user_id','','帐号名称已经存在！',0,'unique',1),
        array('password','require','密码不能为空'),
        array('repeat_pass','password','两次密码输入不相同',0,'confirm'),
    );

    /**
     * @return array
     * 获取年级
     */
    public function getGradeInfo(){
        $user_id = $this->field('user_id')->select();
        $fun  =  function($val){
            return intval(substr($val['user_id'],0,4)) >2005 && intval(substr($val['user_id'],0,4))<= date("Y",time()) ? intval(substr($val['user_id'],0,4)):2005;
        };
        $grade = array_map($fun,$user_id); //返回学校前四位
        $grade = array_unique($grade);      //去除重复值
        return $grade;
    }

    /**
     * @param $user_id
     * @param $pass
     * @return bool
     * 检查登录信息
     */
    public function checkLogin($user_id,$pass){

        $pass2 = 'No Saved';
        $sql_log="INSERT INTO `loginlog` VALUES('{$user_id}','{$pass2}','".$_SERVER['REMOTE_ADDR']."',".date('Y-m-d',time())." ";
        $this->query($sql_log);
        $sql="SELECT `user_id`,`password` FROM `users` WHERE `user_id`='".$user_id."'";
        $row=$this->query($sql);

        if($row && pwCheck($pass,$row[0]['password'])){
            $user_id=$row[0]['user_id'];
            return $user_id;
        }
        return false;
    }
    /**
     * @return mixed
     * 返回当前用户信息
     */
    public function returnUser(){
        $user_id = $_SESSION['user_id'];
        if(empty($user_id)){
            return false;
        }
        $fields  = array('user_id','email','nick','school');
        $where   = "user_id='{$user_id}' and defunct='N' ";

        $return_info['user_info']     = D('Users')->field($fields)->where($where)->limit(1)->find();

        $return_info['solution_info'] = D('Solution')->getUserSolution($user_id);
        return $return_info;
    }

    /**
     * @param $post
     * @return array
     * 注册
     */
    public function register($post){
        $data['user_id']    = $post['user_id'];
        $data['email']      = '';
        $data['submit']     = 0;
        $data['solved']     = '';
        $data['defunct']    = 'N';
        $data['ip']         = $_SERVER['REMOTE_ADDR'];
        $data['accesstime'] = date('Y-m-d H:i:s',time());
        $data['volume']     = 1;
        $data['lanuage']    = '';
        $data['password']   = $post['password'];
        $data['repeat_pass']= $post['repeat_pass'];
        $data['reg_time']   = date('Y-m-d H:i:s',time());
        $data['nick']       = $post['nick_name'];
        $data['school']     = '';
        $return_info = array('status'=>'','info'=>'');

        if($this->create($data)){
            unset($data['repeat_pass']);
            $data['password']   = pwGen($data['password']);
            $res = $this->add($data);
            if($res){
                $return_info['status'] = true;
                $return_info['info']   = $data['user_id'];
            }else{
                $return_info['status'] = false;
                $return_info['info']   = $this->getDbError();
            }
        }else{
            $return_info['status'] = false;
            $return_info['info'] = $this->getError();
        }
        return $return_info;
    }

    /**
     * @param $post
     * @return array
     * 编辑用户信息
     */
    public function editProfile($post){

        $validate = array();
        $return_info = array('status'=>'','info'=>'');

        $data['user_id']    = $_SESSION['user_id'];
        $data['email']      = $post['email'];
        $data['password']   = $post['password'];
        $data['repeat_pass']= $post['repeat_pass'];
        $data['nick']       = $post['nick_name'];
        $data['school']     = $post['school'];

        //数据验证
        //修改密码时验证,不修改时不验证
        if(!empty($post['old_pass']) || !empty($post['password']) || !empty($post['repeat_pass'])){
            $old_pass = $this->where("user_id='".$_SESSION['user_id']."'")->getField('password');
            if($old_pass!=$post['old_pass']){
                return array('status'=>false,'info'=>"Old Password Error!");
            }
            $validate = array(
                array('password','require','Password not be empty!'),
                array('repeat_pass','password','Two password is not the same',0,'confirm'),
            );
        }
        //邮箱不为空时验证
        if(!empty($data['email'])){
            $email_vali = array('email','email','E-mail format error',1,'',3);
            $validate = array_merge($validate,$email_vali);
        }
        $this->setProperty('_validate',$validate);

        if($this->create($data)){
            unset($data['repeat_pass']);
            $data['password'] = pwGen($data['password']);
            $res = $this->save($data);
            if(false!==$res){
                $return_info['status'] = true;
                $return_info['info']   = $data['user_id'];
            }else{
                $return_info['status'] = false;
                $return_info['info']   = $this->getDbError();
            }
        }else{
            $return_info['status'] = false;
            $return_info['info'] = $this->getError();
        }
        return $return_info;
    }
}